package com.qms.repast.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.resbean.SortBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/9/7 0007
 * @describe: 排序适配器
 */
public class SortAdapter extends BaseAdapter {
	private Context context;
	private List<SortBean> sortBeanList;
	private int chooseId = -1;

	public SortAdapter (Context context, List<SortBean> sortBeanList) {
		this.context = context;
		this.sortBeanList = sortBeanList;
	}

	@Override
	public int getCount () {
		return sortBeanList.size();
	}

	@Override
	public Object getItem (int i) {
		return sortBeanList.get(i);
	}

	@Override
	public long getItemId (int i) {
		return i;
	}

	public void setChooseId (int chooseId) {
		this.chooseId = chooseId;
		notifyDataSetChanged();
	}

	@Override
	public View getView (int i, View view, ViewGroup viewGroup) {
		SortBean sortBean = sortBeanList.get(i);
		final ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.layout_sort_item, null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		//TODO
		if (i == chooseId) {
			holder.ivIsShow.setVisibility(View.VISIBLE);
		} else {
			holder.ivIsShow.setVisibility(View.GONE);
		}
		holder.ivIcon.setImageResource(sortBean.getResId());
		holder.tvText.setText(sortBean.getText());
		return view;
	}

	class ViewHolder {
		@BindView(R.id.iv_icon)
		ImageView ivIcon;
		@BindView(R.id.iv_isShow)
		ImageView ivIsShow;
		@BindView(R.id.tv_text)
		TextView tvText;

		public ViewHolder (View view) {
			ButterKnife.bind(this, view);
		}
	}
}

